﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:markup="clr-namespace:Elysium.Markup"
                    xmlns:params="clr-namespace:Elysium.Parameters">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Themes/SharedResources.xaml"/>
        <ResourceDictionary Source="pack://application:,,,/Themes/Selectors/ComboBox.ToggleButton.xaml"/>
        <ResourceDictionary Source="pack://application:,,,/Themes/Selectors/ComboBox.TextBox.xaml"/>
        <ResourceDictionary Source="pack://application:,,,/Themes/Scrolls/ScrollBar.xaml"/>
        <ResourceDictionary Source="pack://application:,,,/Themes/Scrolls/ScrollViewer.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="{markup:ThemeResource BackgroundBrush}"/>
        <Setter Property="BorderBrush" Value="{markup:ThemeResource NormalBrush}"/>
        <Setter Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource Self}, Path=(params:General.DefaultThickness), Mode=OneWay}"/>
        <Setter Property="Foreground" Value="{markup:ThemeResource ForegroundBrush}"/>
        <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource Self}, Path=(params:General.ContentFontSize), Mode=OneWay}"/>
        <Setter Property="FontStretch" Value="Medium"/>
        <Setter Property="FontStyle" Value="Normal"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
        <Setter Property="TextOptions.TextRenderingMode" Value="ClearType"/>
        <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled"/>
        <Setter Property="params:General.ShadowBrush" Value="{markup:ThemeResource BackgroundContrastBrush}"/>
        <Setter Property="Focusable" Value="True"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyle}"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="{Binding RelativeSource={RelativeSource Self}, Path=(params:General.BoldPadding), Mode=OneWay}"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Decorator x:Name="LayoutRoot">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(params:ComboBox.ButtonSize), Converter={StaticResource DoubleToGridLengthConverter}}"/>
                                </Grid.ColumnDefinitions>
                                <Popup x:Name="PART_Popup" 
                                       Grid.Column="0"
                                       Grid.ColumnSpan="2"
                                       AllowsTransparency="True"
                                       IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
                                       PlacementTarget="{Binding ElementName=LayoutRoot}"
                                       Placement="Custom"
                                       VerticalOffset="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}, ConverterParameter='Bottom'}"
                                       CustomPopupPlacementCallback="{x:Static params:DropDown.PopupPlacementCallback}"
                                       PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                                       Tag="{DynamicResource {x:Static SystemParameters.ComboBoxAnimationKey}}">
                                    <Popup.Resources>
                                        <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
                                            <Style.Triggers>
                                                <Trigger Property="Orientation" Value="Vertical">
                                                    <Setter Property="BorderThickness" Value="0"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Popup.Resources>
                                    <Border x:Name="PopupRoot"
                                            Tag="{DynamicResource {x:Static SystemParameters.DropShadowKey}}">
                                        <ScrollViewer x:Name="DropDownScrollViewer"
                                                      Background="{markup:ThemeResource BackgroundBrush}"
                                                      BorderBrush="{TemplateBinding BorderBrush}"
                                                      BorderThickness="{TemplateBinding BorderThickness}"
                                                      Foreground="{TemplateBinding Foreground}"
                                                      FontSize="{TemplateBinding FontSize}"
                                                      FontStretch="{TemplateBinding FontStretch}"
                                                      FontStyle="{TemplateBinding FontStyle}"
                                                      FontWeight="{TemplateBinding FontWeight}"
                                                      TextOptions.TextFormattingMode="{TemplateBinding TextOptions.TextFormattingMode}"
                                                      TextOptions.TextRenderingMode="{TemplateBinding TextOptions.TextRenderingMode}"
                                                      RenderOptions.ClearTypeHint="{TemplateBinding RenderOptions.ClearTypeHint}"
                                                      MinWidth="{Binding ElementName=LayoutRoot, Path=ActualWidth}"
                                                      MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                            <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"/>
                                        </ScrollViewer>
                                    </Border>
                                </Popup>
                                <ToggleButton x:Name="Button"
                                              Grid.Column="0"
                                              Grid.ColumnSpan="2"
                                              IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
                                              Style="{StaticResource ComboBoxToggleButton}"/>
                                <ContentPresenter x:Name="ContentPresenter"
                                                  Grid.Column="0"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  TextBlock.Foreground="{TemplateBinding Foreground}"
                                                  TextBlock.FontSize="{TemplateBinding FontSize}"
                                                  TextBlock.FontStretch="{TemplateBinding FontStretch}"
                                                  TextBlock.FontStyle="{TemplateBinding FontStyle}"
                                                  TextBlock.FontWeight="{TemplateBinding FontWeight}"
                                                  TextOptions.TextFormattingMode="{TemplateBinding TextOptions.TextFormattingMode}"
                                                  TextOptions.TextRenderingMode="{TemplateBinding TextOptions.TextRenderingMode}"
                                                  RenderOptions.ClearTypeHint="{TemplateBinding RenderOptions.ClearTypeHint}"
                                                  IsHitTestVisible="False"
                                                  Margin="{TemplateBinding Padding}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                  Visibility="Visible"/>
                                <Grid x:Name="TextBoxLayout"
                                      Grid.Column="0"
                                      Background="{markup:ThemeResource BackgroundBrush}"
                                      Visibility="Collapsed">
                                    <TextBox x:Name="PART_EditableTextBox"
                                             Foreground="{TemplateBinding Foreground}"
                                             FontFamily="{TemplateBinding FontFamily}"
                                             FontSize="{TemplateBinding FontSize}"
                                             FontStretch="{TemplateBinding FontStretch}"
                                             FontStyle="{TemplateBinding FontStyle}"
                                             FontWeight="{TemplateBinding FontWeight}"
                                             IsReadOnly="{TemplateBinding IsReadOnly}"
                                             Margin="{TemplateBinding Padding}"
                                             Style="{StaticResource ComboBoxTextBox}"/>
                                </Grid>
                            </Grid>
                        </Border>
                    </Decorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEditable" Value="True">
                            <Setter TargetName="Button" Property="Grid.Column" Value="1"/>
                            <Setter TargetName="Button" Property="Grid.ColumnSpan" Value="1"/>
                            <Setter TargetName="ContentPresenter" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="TextBoxLayout" Property="Visibility" Value="Visible"/>
                        </Trigger>
                        <Trigger SourceName="PART_Popup" Property="Tag" Value="False">
                            <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None"/>
                        </Trigger>
                        <Trigger Property="IsDropDownOpen" Value="True">
                            <Setter TargetName="PART_Popup" Property="params:DropDown.IsOpen" Value="True"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="PART_Popup" Property="params:DropDown.IsDefaultHorizontalPosition" Value="True"/>
                                <Condition SourceName="PART_Popup" Property="params:DropDown.IsDefaultVerticalPosition" Value="True"/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter TargetName="DropDownScrollViewer" Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Mode=OneWay, Converter={StaticResource ThicknessConverter}, ConverterParameter='Left, Right, Bottom'}"/>
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="PART_Popup" Property="params:DropDown.IsDefaultHorizontalPosition" Value="True"/>
                                <Condition SourceName="PART_Popup" Property="params:DropDown.IsDefaultVerticalPosition" Value="False"/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter TargetName="DropDownScrollViewer" Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Mode=OneWay, Converter={StaticResource ThicknessConverter}, ConverterParameter='Left, Top, Right'}"/>
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=PopupRoot, Path=Tag}" Value="True"/>
                                <Condition Binding="{Binding ElementName=PART_Popup, Path=(params:DropDown.IsDefaultVerticalPosition)}" Value="True"/>
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter TargetName="PopupRoot" Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Color="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, Path=(params:General.ShadowBrush).(SolidColorBrush.Color)}"
                                                          BlurRadius="4" ShadowDepth="2"
                                                          Direction="315"
                                                          Opacity="0.3"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter TargetName="PopupRoot" Property="Background" Value="{markup:ThemeResource TransparentBrush}"/>
                                <Setter TargetName="PopupRoot" Property="Padding" Value="0,0,6,6"/>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=PopupRoot, Path=Tag}" Value="True"/>
                                <Condition Binding="{Binding ElementName=PART_Popup, Path=(params:DropDown.IsDefaultVerticalPosition)}" Value="False"/>
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter TargetName="PopupRoot" Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Color="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, Path=(params:General.ShadowBrush).(SolidColorBrush.Color)}"
                                                          BlurRadius="4" ShadowDepth="0"
                                                          Direction="45"
                                                          Opacity="0.3"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter TargetName="PopupRoot" Property="Background" Value="{markup:ThemeResource TransparentBrush}"/>
                                <Setter TargetName="PopupRoot" Property="Padding" Value="0,4,4,0"/>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </ControlTemplate.Triggers>                    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Mode=OneWay, Converter={StaticResource IsGreaterThanConverter}, ConverterParameter=18}">
                <Setter Property="TextOptions.TextFormattingMode" Value="Ideal"/>
            </DataTrigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="BorderBrush" Value="{markup:ThemeResource DisabledBrush}"/>
                <Setter Property="Foreground" Value="{markup:ThemeResource DisabledBrush}"/>
            </Trigger>
            <Trigger Property="IsGrouping" Value="True">
                <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{markup:ThemeResource PaleBrush}"/>
            </Trigger>
            <Trigger Property="IsDropDownOpen" Value="True">
                <Setter Property="Background" Value="{markup:ThemeResource PaleBrush}"/>
            </Trigger>
        </Style.Triggers>        
    </Style>

</ResourceDictionary>